package co.fitstart.entity.finance;

import java.beans.Transient;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.order.Order;
import co.fitstart.entity.order.PaymentMethod;

/**
 * 
 * @author dan
 *
 */
@Entity
@Table(name = "receipt")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class Receipt {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 单据类型 */
    private ReceiptType type;
    
    /** 付款类型 */
    private PaidType paidType;
    
    /** 单据编号 */
    private String code;
    
    /** 单据日期 */
    private Date createTime;
    
    /** 客户编码 */
    private String studentCode;
    
    /** 结算方式 */
    private String paymentMethod;
    
    /** 汇率 */
    private BigDecimal exchangeRate;
    
    /** 本币金额 */
    private int domesticPrice;
    
    /** 原币金额 */
    private int abroadPrice;
    
    /** 本币余额 */
    private int domesticBalance;
    
    /** 原币余额 */
    private int abroadBalance;
    
    /** 部门编码 */
    private String siteCode;
    
    /** 业务员编码 */
    private String salesmanCode;
    
    /** 项目大类编号 */
    private String classNumber;
    
    /** 项目编号 */
    private String number;
    
    /** 项目名称 */
    private String productName;
    
    /** 订单号 */
    private String orderCode;
    
    /** 录入人 */
    private String entryPerson;
    
    /** 是否导出 */
    private boolean export;
    
    /**
     * 
     */
    public Receipt() {
        
    }
    
    /**
     * 
     * @param order
     * @param type
     * @param paymentMethod
     * @param exchangeRate
     * @param price
     * @param entryPerson
     */
    public Receipt(Order order, ReceiptType type, PaidType paidType, PaymentMethod paymentMethod, BigDecimal exchangeRate, int price, String entryPerson) {
        this.type = type;
        this.paidType = paidType;
        this.code = order.getCode();
        this.createTime = new Date();
        this.studentCode = "RMJU0" + order.getUser().getId();
        this.paymentMethod = paymentMethod.getCode();
        this.exchangeRate = exchangeRate;
        this.domesticPrice = price;
        this.abroadPrice = price;
        this.domesticBalance = order.getPrice() - order.getPaidPrice();
        this.abroadBalance = order.getPrice() - order.getPaidPrice();
        this.siteCode = order.getOrderType().isOnline() ? "H06" : order.getOrderType().isOffline() ? order.getSite().getFinanceCode() : "H02";
        this.salesmanCode = order.getOrderType().isOnline() ? "017" : order.getOrderType().isOthers() ? "054" : order.getSalesman() != null ?  order.getSalesman().getFinanceCode() : "";
        this.classNumber = order.getOrderType().isOnline() ? "RMJZXS" : order.getOrderType().isOffline() ? "RMJZXX" : "RMJZDS";
        this.number = this.classNumber + order.getProduct().getId();
        this.productName = order.getProduct().getName();
        this.orderCode = order.getCode();
        this.entryPerson = entryPerson;
        this.export = false;
    }
    
    /**
     * 
     * @param order
     * @param type
     * @param paidType
     * @param price
     * @param entryPerson
     */
    public Receipt(Order order, ReceiptType type, PaidType paidType, int price, String entryPerson) {
        this.type = type;
        this.paidType = paidType;
        this.code = order.getCode();
        this.createTime = new Date();
        this.studentCode = "RMJU0" + order.getUser().getId();
        this.paymentMethod = order.getPaymentMethod().getCode();
        this.exchangeRate = new BigDecimal(1);
        this.domesticPrice = price;
        this.abroadPrice = price;
        this.domesticBalance = order.getPrice() - order.getPaidPrice();
        this.abroadBalance = order.getPrice() - order.getPaidPrice();
        this.siteCode = order.getOrderType().isOnline() ? "H06" : order.getOrderType().isOffline() ? order.getSite().getFinanceCode() : "H02";
        this.salesmanCode = order.getOrderType().isOnline() ? "017" : order.getOrderType().isOthers() ? "054" : order.getSalesman() != null ?  order.getSalesman().getFinanceCode() : "";
        this.classNumber = order.getOrderType().isOnline() ? "RMJZXS" : order.getOrderType().isOffline() ? "RMJZXX" : "RMJZDS";
        this.number = this.classNumber + order.getProduct().getId();
        this.productName = order.getProduct().getName();
        this.orderCode = order.getCode();
        this.entryPerson = entryPerson;
        this.export = false;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String getGeneralPaymentMethod() {
        return PaymentMethod.getDescriptionByCode(this.paymentMethod);
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the type
     */
    public ReceiptType getType() {
        return type;
    }

    /**
     * @return the paidType
     */
    public PaidType getPaidType() {
        return paidType;
    }

    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the studentCode
     */
    public String getStudentCode() {
        return studentCode;
    }

    /**
     * @return the paymentMethod
     */
    public String getPaymentMethod() {
        return paymentMethod;
    }

    /**
     * @return the exchangeRate
     */
    public BigDecimal getExchangeRate() {
        return exchangeRate;
    }

    /**
     * @return the domesticPrice
     */
    public int getDomesticPrice() {
        return domesticPrice;
    }

    /**
     * @return the abroadPrice
     */
    public int getAbroadPrice() {
        return abroadPrice;
    }

    /**
     * @return the domesticBalance
     */
    public int getDomesticBalance() {
        return domesticBalance;
    }

    /**
     * @return the abroadBalance
     */
    public int getAbroadBalance() {
        return abroadBalance;
    }

    /**
     * @return the siteCode
     */
    public String getSiteCode() {
        return siteCode;
    }

    /**
     * @return the salesmanCode
     */
    public String getSalesmanCode() {
        return salesmanCode;
    }

    /**
     * @return the classNumber
     */
    public String getClassNumber() {
        return classNumber;
    }

    /**
     * @return the number
     */
    public String getNumber() {
        return number;
    }

    /**
     * @return the productName
     */
    public String getProductName() {
        return productName;
    }

    /**
     * @return the orderCode
     */
    public String getOrderCode() {
        return orderCode;
    }

    /**
     * @return the entryPerson
     */
    public String getEntryPerson() {
        return entryPerson;
    }

    /**
     * @return the export
     */
    public boolean isExport() {
        return export;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param type the type to set
     */
    public void setType(ReceiptType type) {
        this.type = type;
    }

    /**
     * @param paidType the paidType to set
     */
    public void setPaidType(PaidType paidType) {
        this.paidType = paidType;
    }

    /**
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param studentCode the studentCode to set
     */
    public void setStudentCode(String studentCode) {
        this.studentCode = studentCode;
    }

    /**
     * @param paymentMethod the paymentMethod to set
     */
    public void setPaymentMethod(String paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    /**
     * @param exchangeRate the exchangeRate to set
     */
    public void setExchangeRate(BigDecimal exchangeRate) {
        this.exchangeRate = exchangeRate;
    }

    /**
     * @param domesticPrice the domesticPrice to set
     */
    public void setDomesticPrice(int domesticPrice) {
        this.domesticPrice = domesticPrice;
    }

    /**
     * @param abroadPrice the abroadPrice to set
     */
    public void setAbroadPrice(int abroadPrice) {
        this.abroadPrice = abroadPrice;
    }

    /**
     * @param domesticBalance the domesticBalance to set
     */
    public void setDomesticBalance(int domesticBalance) {
        this.domesticBalance = domesticBalance;
    }

    /**
     * @param abroadBalance the abroadBalance to set
     */
    public void setAbroadBalance(int abroadBalance) {
        this.abroadBalance = abroadBalance;
    }

    /**
     * @param siteCode the siteCode to set
     */
    public void setSiteCode(String siteCode) {
        this.siteCode = siteCode;
    }

    /**
     * @param salesmanCode the salesmanCode to set
     */
    public void setSalesmanCode(String salesmanCode) {
        this.salesmanCode = salesmanCode;
    }

    /**
     * @param classNumber the classNumber to set
     */
    public void setClassNumber(String classNumber) {
        this.classNumber = classNumber;
    }

    /**
     * @param number the number to set
     */
    public void setNumber(String number) {
        this.number = number;
    }

    /**
     * @param productName the productName to set
     */
    public void setProductName(String productName) {
        this.productName = productName;
    }

    /**
     * @param orderCode the orderCode to set
     */
    public void setOrderCode(String orderCode) {
        this.orderCode = orderCode;
    }

    /**
     * @param entryPerson the entryPerson to set
     */
    public void setEntryPerson(String entryPerson) {
        this.entryPerson = entryPerson;
    }

    /**
     * @param export the export to set
     */
    public void setExport(boolean export) {
        this.export = export;
    }

}
